using System;
using GameKit.UI;
using UnityEngine;

namespace GameKitUnity.UI
{
    public class UIPanel : MonoBehaviour, IPanel
    {
        public string Name => gameObject.name;
        public int LayerIndex { get; set; }
        public event Action OnDestroyEvent;

        private void Awake()
        {
            var rectTransform = GetComponent<RectTransform>();
            rectTransform.Stretch();
        }

        public void Open()
        {
            var mask = new GameObject("Mask");
            mask.transform.SetParent(transform);
            mask.transform.SetAsFirstSibling();
            mask.AddComponent<UIMask>();
        }

        public void Close()
        {
            Destroy(gameObject);
        }

        private void OnDestroy()
        {
            OnDestroyEvent?.Invoke();
        }
    }
}